package dados;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import basicas.Administrador;
import intefaces.InterfaceAdministrador;

public class DadosAdministrador extends MySQLConnector implements InterfaceAdministrador {

	@Override
	public void inserir(Administrador adm) throws Exception {
		
		try{
			dbConn = dbConn();
			dbConn.setAutoCommit(false);
	
			String sql = "INSERT INTO Administrador (nome, email, senha, root) VALUES (?, ?, ?, ?)";
			
			preparedStatementInsert = dbConn.prepareStatement(sql);
			preparedStatementInsert.setString(1, adm.getNome());
			preparedStatementInsert.setString(2, adm.getEmail());
			preparedStatementInsert.setString(3, adm.getSenha());
			preparedStatementInsert.setBoolean(4, adm.isRoot());
			preparedStatementInsert.executeUpdate();
			
			dbConn.commit();
			
		}catch(SQLException ex){
			dbConn.rollback();
		}finally{
			if(preparedStatementInsert != null){
				preparedStatementInsert.close();
			}
			if(dbConn != null){
				dbConn.close();
			}	
		}
	}

	@Override
	public void alterar(Administrador adm) throws Exception {


		try{
			dbConn = dbConn();
			dbConn.setAutoCommit(false);
	
			String sql = "UPDATE Administrador SET nome = ?, email = ?, senha = ?, root =? WHERE id_admin = ?";
			
			preparedStatementInsert = dbConn.prepareStatement(sql);
			preparedStatementInsert.setString(1, adm.getNome());
			preparedStatementInsert.setString(2, adm.getEmail());
			preparedStatementInsert.setString(3, adm.getSenha());
			preparedStatementInsert.setBoolean(4, adm.isRoot());
			preparedStatementInsert.setInt(5, adm.getId_admin());
			preparedStatementInsert.executeUpdate();
			
			dbConn.commit();
			
		}catch(SQLException ex){
			dbConn.rollback();
		}finally{
			if(preparedStatementInsert != null){
				preparedStatementInsert.close();
			}
			if(dbConn != null){
				dbConn.close();
			}	
		}

	}

	@Override
	public void excluir(Administrador adm) throws Exception {


		try{
			dbConn = dbConn();
			dbConn.setAutoCommit(false);
	
			String sql = "DELETE FROM Administrador  WHERE id_admin = ?";
			
			preparedStatementInsert = dbConn.prepareStatement(sql);
			preparedStatementInsert.setInt(1, adm.getId_admin());
			preparedStatementInsert.executeUpdate();
			
			dbConn.commit();
			
		}catch(SQLException ex){
			dbConn.rollback();
		}finally{
			if(preparedStatementInsert != null){
				preparedStatementInsert.close();
			}
			if(dbConn != null){
				dbConn.close();
			}	
		}

	}

	@Override
	public List<Administrador> listarTodos() throws Exception {
		List<Administrador> adms = new ArrayList<Administrador>();
		logar();
		try{
		String sql = "SELECT id, nome, email, senha, root FROM Administrador";
		ResultSet rs = logar().executeQuery(sql);
		while(rs.next()){
			Administrador adm = new Administrador();
			adm.setId_admin(rs.getInt("id_admin"));
			adm.setNome(rs.getString("nome"));
			adm.setEmail(rs.getString("email"));
			adm.setSenha(rs.getString("senha"));
			adm.setRoot(rs.getBoolean("root"));
			adms.add(adm);
		}
			deslogar();
			return adms;
		}catch(Exception ex){
			throw new Exception(ex.getMessage());
		}
	}

}
